/**
 * 
 */
package com.effectiv.req1.event;

import jodd.mail.Email;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.effectiv.req1.domain.User;
import com.effectiv.req1.email.EmailBodyPreparer;
import com.effectiv.req1.email.EmailOperationTemplate;
import com.google.common.eventbus.Subscribe;

/**
 * @author Dhrubo
 * 
 */
@Component
@Slf4j
public class UserEventHandler implements EventHandler {

	@Autowired
	private EmailOperationTemplate emailOperationTemplate;

	

	@Subscribe
	public void registerUser(RegistrationEvent event) {
		EmailBodyPreparer<User> emailBodyPreparer = new EmailBodyPreparer<User>(event.getUser(), "registration", event.getLocale());
		
		Email email = Email.create()
                .to(event.getUser().getEmail())
                .from("info@gooruda.com")
                .subject("Gooruda - Please activiate your account");

		
		emailOperationTemplate.send(email, emailBodyPreparer);
	}

	@Subscribe
	public void activate(UserActivationEvent event) {
		EmailBodyPreparer<User> emailBodyPreparer = new EmailBodyPreparer<User>(event.getUser(), "activation", event.getLocale());
		
		Email email = Email.create()
                .to(event.getUser().getEmail())
                .from("info@gooruda.com")
                .subject("Gooruda - Your account details");

		
		emailOperationTemplate.send(email, emailBodyPreparer);
	}
}
